<?php

namespace app\api\controller;

use app\api\model\CircleCategory;
use app\api\model\CircleCommentReplies;
use app\api\model\CircleComments;
use app\api\model\CircleLikes;
use app\api\model\Circles;
use app\api\model\Designs;
use app\common\controller\Api;
use think\Controller;
use think\Request;

/**
 * 拾玖圈
 */
class Circle extends Api
{

    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    /**
     * 拾玖圈
     */
    public function index()
    {
        $length = $this->request->get('length');
        $category_id = $this->request->get('category_id');
        $circles = Circles::getCircles($category_id, $length);
        $this->items($circles);
    }

    /**
     * 拾玖圈详情
     */
    public function read($circle_id)
    {
        $circle = Circles::getCircleById($circle_id);
        if (!$circle) {
            $this->error('没有数据', [], 2);
        }
        $design_count = Circles::getCircleCount($circle->design_id);
        $circle->design_count = $design_count;
        //是否已点赞
        $like = CircleLikes::getCircleLike($this->auth->getUserId(), $circle_id);
        $circle->like = $like ? 1 : 0;
        $this->success('请求成功', ['circle' => $circle]);
    }

    /**
     * 评论
     */
    public function getComment($circle_id)
    {
        $comments = CircleComments::getCommentsByCircelId($circle_id, $this->request->get('length'));
        $comments->items() ? $this->success('请求成功', ['comments' => $comments->items()]) : $this->error('没有数据', [], 2);
    }

    /**
     * 点赞
     */
    public function like($circle_id)
    {
        $user_id = $this->auth->getUserId();
        $circleLike = CircleLikes::getCircleLike($user_id, $circle_id);
        if ($circleLike) {
            $res = $circleLike->delete();
        } else {
            $res = CircleLikes::create([
                'user_id' => $user_id,
                'circle_id' => $circle_id
            ]);
        }
        $res ? $this->success('请求成功') : $this->error('请求失败');
    }

    /**
     * 评论
     */
    public function comment($circle_id)
    {
        $content = $this->request->post('content');
        if (empty($content)) {
            $this->error('评论内容不能为空');
        }
        $res = CircleComments::create([
            'content' => $content,
            'user_id' => $this->auth->getUserId(),
            'circle_id' => $circle_id
        ]);
        $res ? $this->success('请求成功') : $this->error('请求失败');
    }

    /**
     * 回复
     */
    public function reply($comment_id)
    {
        $content = $this->request->post('content');
        if (empty($content)) {
            $this->error('评论内容不能为空');
        }
        $res = CircleCommentReplies::create([
            'content' => $content,
            'user_id' => $this->auth->getUserId(),
            'comment_id' => $comment_id
        ]);
        $res ? $this->success('请求成功') : $this->error('请求失败');
    }

    /**
     * 造型师作品集
     */
    public function design($design_id)
    {
        $length = $this->request->get('length');
        $circles = Circles::getCirclesByDesignId($design_id, $length);
        $this->items($circles);
    }

    private function items($circles)
    {
        if (!$circles->items()) {
            $this->error('没有数据', [], 2);
        }
        foreach ($circles->items() as $key=>$val) {
            $circleLike = CircleLikes::getCircleLike($this->auth->getUserId(), $val->id);
            $val->like = $circleLike ? 1 : 0;
        }
        $this->success('请求成功', ['circles' => $circles->items()]);
    }

    /**
     * 分类
     */
    public function category()
    {
        $categories = CircleCategory::getCategories();
        $categories ? $this->success('请求成功', compact('categories')) : $this->error('没有数据', [], 2);
    }
}
